//
//  ZLVideoPlayer.m
//  ZLMediaPlayer
//
//  Created by zhangliang on 2017/4/18.
//  Copyright © 2017年 zhangliang. All rights reserved.
//
//1.AVPlayer 用于播放音视频
//2.AVPlayerItem 音频的对象
//3.AVPlayerLayer 播放显示视频的图层界面
//4.AVPlayerViewController 他会帮你去创建显示视频的图层 有调节控件
#import "ZLVideoPlayerView.h"
#import <AVFoundation/AVFoundation.h>



@interface ZLVideoPlayerView ()

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;

@end

@implementation ZLVideoPlayerView

+ (instancetype)shareInstance {
    static ZLVideoPlayerView *playerView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playerView = [[ZLVideoPlayerView alloc]init];
    });
    return playerView;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame URL:(NSURL *)url {
    self = [super initWithFrame:frame];
    if (self) {
        NSString *mp4url = @"http://101.227.98.44/cosvideo.52doushi.com/static/video/11/27/1342368_1492422013052.mp4?key1=95e502945d037d0098ca5c0ebd74c27b&key2=58f7026a&sign=r+nuWQS0Fwlc8clPkF8FOg23LzBhPTEyNTI0MjQ0Nzkmaz1BS0lEVEN3V3ZGSE5Cb0dXYVNOcEVXZHUwdWM5WGprWW5jcnQmZT0xNDkyNTgzMDE4JnQ9MTQ5MjQ5NjYxOCZyPTEyMDg4Mjk3NzQyNTYwNjA3OTQ0JmY9JmI9ZHN2aWRlbw==&wsiphost=local";
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"194501150417163273816451.mp4" withExtension:nil];
        url = [NSURL URLWithString:mp4url];
        //playerItemWithAsset:通过设备相册里面的内容 创建一个 要播放的对象
        //我们这里直接选择使用URL读取
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
        
        //    duration   当前播放元素的总时长
        //    status  加载的状态
        //          AVPlayerItemStatusUnknown,  未知状态
        //          AVPlayerItemStatusReadyToPlay,  准备播放的状态
        //          AVPlayerItemStatusFailed   失败的状态
        //    时间控制的类目
        //    current
        //    forwordPlaybackEndTime   跳到结束位置
        //    reversePlaybackEndTime    跳到开始位置
        //    seekToTime   跳到指定位置
        
        _player = [AVPlayer playerWithPlayerItem:item];
        //也可以直接WithURL来获得一个地址的视频文件
        //    externalPlaybackVideoGravity    视频播放的样式
        //      AVLayerVideoGravityResizeAspect   普通的
        //      AVLayerVideoGravityResizeAspectFill   充满的
        //      currentItem  获得当前播放的视频元素
        
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
        playerLayer.frame = self.frame;
        [self.layer addSublayer:playerLayer];
        
        // 注意的是使用这个类需要导入AVKit框架
//        AVPlayerViewController * vc = [[AVPlayerViewController alloc] init];
//        vc.player = player;
//        vc.view.frame = self.view.frame;
//        [self.view addSubview:vc.view];
        _player.externalPlaybackVideoGravity = AVLayerVideoGravityResizeAspectFill;
    }
    return self;
}

- (void)play {
    [_player play];
}

- (void)resume {
    [_player pause];
}



@end


//- (instancetype)initWithFrame:(CGRect)frame URL:(NSURL *)url {
//    self = [super initWithFrame:frame];
//    if (self) {
//        
//    }
//    return self;
//}
